package org.dromara.tearoom.mapper;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.dromara.tearoom.domain.TerAccounts;
import org.dromara.tearoom.domain.vo.TerAccountsVo;
import org.dromara.common.mybatis.core.mapper.BaseMapperPlus;

import java.time.LocalDateTime;

/**
 * 账户Mapper接口
 *
 * @author xch
 * @date 2025-05-07
 */
public interface TerAccountsMapper extends BaseMapperPlus<TerAccounts, TerAccountsVo> {
    @Update("UPDATE ter_accounts SET balance = balance + #{amount},gift_amount = gift_amount+ #{giftAmount}, version = version + 1 " +
            "WHERE id = #{id} AND version = #{version}")
    int updateBalance(@Param("id") Long id,
                      @Param("amount") Integer amount,
                      @Param("giftAmount") Integer giftAmount,
                      @Param("version") Integer version);

    @Update("UPDATE ter_accounts SET balance = balance - #{amount},gift_amount = gift_amount- #{giftAmount},integral =integral+ (#{amount} + #{giftAmount})/100 , version = version + 1 WHERE id = #{id} AND version = #{version}")
    int updateDecBalance(@Param("id") Long id,@Param("amount") Integer amount,@Param("giftAmount") Integer giftAmount,
                         @Param("version") Integer version);

    @Update("UPDATE ter_accounts SET balance = #{amount},gift_amount = #{giftAmount}, version = version + 1 WHERE id = #{id} AND version = #{version}")
    int updateOffsetAmount(@Param("id") Long id,@Param("amount") Integer amount,@Param("giftAmount") Integer giftAmount,
                         @Param("version") Integer version);

    @Select("select COALESCE(SUM(balance), 0) from ter_accounts where  create_time >= #{start} AND create_time < DATE_ADD( #{end}, INTERVAL 1 DAY)")
    int selectBalance(@Param("start") String start, @Param("end") String end);
}
